<template>
  <div id="app">
    <TheNavbar />
    <div class="home-container">
      <div class="greeting-box loaded">
        <div class="greeting-icon">👋</div>
        <div class="greeting-text">
          {{ greetingMessage }}
        </div>
        <div class="nickname">
          {{ nickname }}
        </div>
      </div>
      <div class="job-list-box loaded">
        <div class="job-list-title">
          热门岗位
        </div>
        <!-- 添加滚动容器 -->
        <div class="job-list-scrollable">
          <div class="job-list-content">
            <div
              v-for="(jobs, category) in jobCategories"
              :key="category"
              class="job-category-card"
            >
              <div class="category-header">{{ category }}</div>
              <div class="job-items">
                <span
                  v-for="job in jobs"
                  :key="job"
                  class="job-item"
                  @click="searchByTag(job)"
                >
                  {{ job }}
                </span>
              </div>
              <div class="view-more">查看更多 &gt;</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import apiClient from '@/utils/api'
import TheNavbar from '@/components/AppNavbar.vue'
import { useRouter } from 'vue-router'

export default {
  name: 'HomeView',
  components: {
    TheNavbar
  },
  data () {
    return {
      greetingMessage: '',
      nickname: '',
      jobCategories: {}
    }
  },
  setup () {
    const router = useRouter()
    return {
      router
    }
  },
  created () {
    this.updateGreetingMessage()
    this.fetchNickname()
    this.fetchJobList()
  },
  mounted () {
    // 添加加载动画类
    this.$el.querySelector('.greeting-box').classList.add('loaded')
    this.$el.querySelector('.job-list-box').classList.add('loaded')
  },
  methods: {
    updateGreetingMessage () {
      const currentHour = new Date().getHours()
      if (currentHour >= 6 && currentHour < 12) {
        this.greetingMessage = '早上好！'
      } else if (currentHour >= 12 && currentHour < 18) {
        this.greetingMessage = '中午好！'
      } else {
        this.greetingMessage = '晚上好！'
      }
    },
    fetchNickname () {
      const userInfoStr = localStorage.getItem('userInfo')
      if (userInfoStr) {
        const userInfo = JSON.parse(userInfoStr)
        this.nickname = userInfo.nickname || 'Guest'
      } else {
        this.nickname = 'Guest'
      }
    },
    async fetchJobList () {
      try {
        const response = await apiClient.get('/user/home')
        if (response.data.code === '200') {
          this.jobCategories = response.data.data
        } else {
          console.error('Failed to fetch job list:', response.data.msg)
        }
      } catch (error) {
        console.error('Error fetching job list:', error)
      }
    },
    searchByTag (tagName) {
      // 跳转到职位列表页面，并传递标签参数
      this.router.push({
        path: '/jobList',
        query: { tagName: tagName }
      })
    }
  }
}
</script>

<style scoped>
#app {
  background: linear-gradient(to right, #e0f7fa, #ffffff);
  min-height: 100vh;
  padding: 20px;
}

.home-container {
  display: flex;
  justify-content: space-between;
  padding: 30px;
  gap: 30px;
  flex-wrap: wrap;
}

.greeting-box, .job-list-box {
  background-color: #fff;
  border: 1px dashed #a0d9e7;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  width: 48%;
  margin-bottom: 20px;
  opacity: 0;
  transform: translateY(20px);
  transition: opacity 0.6s ease, transform 0.6s ease;
}

.greeting-box.loaded, .job-list-box.loaded {
  opacity: 1;
  transform: translateY(0);
}

.greeting-box {
  background-color: #e6f7ff;
  border-color: #409eff;
  color: #333;
}

.greeting-icon {
  font-size: 32px;
  margin-bottom: 10px;
}

.greeting-text {
  font-size: 32px;
  line-height: 1.2;
  color: #333;
  margin-bottom: 10px;
}

.nickname {
  font-size: 20px;
  color: #555;
}

.job-list-title {
  font-size: 22px;
  color: #333;
  margin-bottom: 10px;
  text-align: center;
}

/* 添加滚动容器样式 */
.job-list-scrollable {
  max-height: 400px;
  overflow-y: auto;
  padding-right: 5px;
}

/* 美化滚动条 */
.job-list-scrollable::-webkit-scrollbar {
  width: 6px;
}

.job-list-scrollable::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 10px;
}

.job-list-scrollable::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 10px;
}

.job-list-scrollable::-webkit-scrollbar-thumb:hover {
  background: #a1a1a1;
}

.job-list-content {
  display: flex;
  flex-direction: column;
}

.job-category-card {
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 15px;
  margin-bottom: 15px;
  transition: box-shadow 0.3s ease;
}

.job-category-card:hover {
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.category-header {
  font-weight: bold;
  font-size: 16px;
  margin-bottom: 8px;
  color: #333;
}

.job-items {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.job-item {
  background-color: #e6f7ff;
  color: #007acc;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
}

.job-item:hover {
  background-color: #1890ff;
  color: white;
}

.view-more {
  color: #007acc;
  font-size: 14px;
  cursor: pointer;
}

@media (max-width: 768px) {
  .greeting-box, .job-list-box {
    width: 100%;
  }

  .greeting-text {
    font-size: 28px;
  }

  .nickname {
    font-size: 18px;
  }

  .job-list-title {
    font-size: 20px;
  }

  .job-list-scrollable {
    max-height: 300px;
  }
}
</style>
